var button = document.getElementById("copy-button"),
contentHolder = document.getElementById("content-holder");
button.addEventListener("click", function() {
// We will need a range object and a selection.
var range = document.createRange(),
selection = window.getSelection();
// Clear selection from any previous data.
selection.removeAllRanges();
// Make the range select the entire content of the contentHolder paragraph.
range.selectNodeContents(contentHolder);
// Add that range to the selection.
selection.addRange(range);
// Copy the selection to clipboard.
document.execCommand('copy');
// Clear selection if you want to.
selection.removeAllRanges();
}, false);
<p id="content-holder">This text will be inserted into the clipboard.</p>
<button id="copy-button">Copy Text</button>
<textarea placeholder="Paste here"></textarea>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
padding: 20px;
font: normal 16px sans-serif;
color: #555;
}
p{
margin-bottom: 20px;
}
button{
padding: 8px 12px;
margin-bottom: 20px;
}
textarea {
display: block;
padding: 10px;
width: 400px;
height: 120px;
}
In the example above the content we want to copy is simply stored in a paragraph. If the text you need is not on the page, you will need to first write it in an element hidden off-screen.
Modify Copied Text
Here we will show you how to manipulate content in the clipboard after it's been copied. This can be very useful for escaping code, formatting numbers and dates, or for other text transformations such as uppercase, lowercase, etc.
JavaScript provides us with
copy()
and
paste()
events, but they are designed in such a way that the content stored in the clipboard is secure:
In the
copy
event handler we cannot read what's stored in clipboard, as there may be personal info which we shouldn't have access to. We can, however, overwrite the clipboard data.
In the
paste
event it's the opposite: we can read the data, but we cannot change it.
Since we want to read and write at the same time, we will need to use the Selection API once more. Here is the solution:
document.addEventListener('copy', function(e){
// We need to prevent the default copy functionality,
// otherwise it would just copy the selection as usual.
e.preventDefault();
// The copy event doesn't give us access to the clipboard data,
// so we need to get the user selection via the Selection API.
var selection = window.getSelection().toString();
// Transform the selection in any way we want.
// In this example we will escape HTML code.
var escaped = escapeHTML(selection);
// Place the transformed text in the clipboard.
e.clipboardData.setData('text/plain', escaped);
});
You can try the code in the editor below:
document.addEventListener('copy', function(e){
// We need to prevent the default copy functionality,
// otherwise it would just copy the selection as usual.
e.preventDefault();
// The copy event doesn't give us access to the clipboard data,
// so we need to get the user selection via the Selection API.
var selection = window.getSelection().toString();
// Transform the selection in any way we want.
var escaped = escapeHtml(selection);
// Place the transformed text in the clipboard.
e.clipboardData.setData('text/plain', escaped);
});
// Primitive HTML escape function.
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
<p class="copy-text">Copy and paste any of the HTML code below to escape it.</p>
<pre><code>
<div class="container">
<div class="starter-template">
<h1>Lorem Ipsum</h1>
<p class="lead">Lorem ipsum dolor sit amet.</p>
</div>
</div>
</code></pre>
<textarea class="paste-text" placeholder="Paste here"></textarea>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
padding: 20px;
font: normal 16px sans-serif;
color: #555;
}
pre{
font-size: 14px;
margin-bottom: 20px;
}
textarea {
padding: 10px;
width: 500px;
height: 170px;
}
Further Reading
In this quick tip we presented you two useful snippets for working with the clipboard in pure vanilla JavaScript. We used a bunch of hip native APIs, so here they are again if you want to read more about them:
execCommand - Execute actions such as copy, paste, cut, bold, italic, underline, delete, and many others. -
MDN
,
Can I Use
Selection API - Allows developers to make a range selection from any text on the page. -
MDN
,
Can I Use
JavaScript Copy Event - An event fired when users press CTRL/Cmd+C or choose "copy" from the right-click menu. -
MDN
,
Can I Use
Also, if you need more control over copy/paste/cut events, you can use a library such as
clipobard.js
.
It has lots of features and provides a nice clean API for managing the clipboard.